/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Copyright (C) 2003-05  Jimm Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/jimm/comm/OtherAction.java
 Version: ###VERSION###  Date: ###DATE###
 Author(s): aspro
 *******************************************************************************/

package jimm.comm;

import java.util.Vector;
import jimm.JimmException;
import javax.microedition.lcdui.Image;
//import jimm.MainMenu;
import jimm.Options;
import jimm.comm.ConnectAction;
import jimm.comm.GUID;
import jimm.comm.Icq;
import jimm.comm.Util;
import jimm.comm.XStatus;
import jimm.comm.SnacPacket;

public class OtherAction 
{
    
    /** Creates a new instance of OtherAction */
    public OtherAction() {
    }
    
    public static final GUID CAP_AIM_SERVERRELAY = new GUID(new byte[]{(byte)0x09, (byte)0x46, (byte)0x13, (byte)0x49, (byte)0x4C, (byte)0x7F, (byte)0x11, (byte)0xD1, (byte)0x82, (byte)0x22, (byte)0x44, (byte)0x45, (byte)0x53, (byte)0x54, (byte)0x00, (byte)0x00});
    public static final GUID CAP_AIM_ISICQ       = new GUID(new byte[]{(byte)0x09, (byte)0x46, (byte)0x13, (byte)0x44, (byte)0x4C, (byte)0x7F, (byte)0x11, (byte)0xD1, (byte)0x82, (byte)0x22, (byte)0x44, (byte)0x45, (byte)0x53, (byte)0x54, (byte)0x00, (byte)0x00});
    public static final GUID CAP_UNKNOWN         = new GUID(new byte[]{(byte)0x09, (byte)0x46, (byte)0x00, (byte)0x00, (byte)0x4C, (byte)0x7F, (byte)0x11, (byte)0xD1, (byte)0x82, (byte)0x22, (byte)0x44, (byte)0x45, (byte)0x53, (byte)0x54, (byte)0x00, (byte)0x00});
    public static final GUID CAP_UTF8            = new GUID(new byte[]{(byte)0x09, (byte)0x46, (byte)0x13, (byte)0x4E, (byte)0x4C, (byte)0x7F, (byte)0x11, (byte)0xD1, (byte)0x82, (byte)0x22, (byte)0x44, (byte)0x45, (byte)0x53, (byte)0x54, (byte)0x00, (byte)0x00});
    public static final GUID CAP_MTN             = new GUID(new byte[]{(byte)0x56, (byte)0x3f, (byte)0xc8, (byte)0x09, (byte)0x0b, (byte)0x6f, (byte)0x41, (byte)0xbd, (byte)0x9f, (byte)0x79, (byte)0x42, (byte)0x26, (byte)0x09, (byte)0xdf, (byte)0xa2, (byte)0xf3});
    public static final GUID CAP_FILE_TRANSFER   = new GUID(new byte[]{(byte)0x09, (byte)0x46, (byte)0x13, (byte)0x43, (byte)0x4C, (byte)0x7F, (byte)0x11, (byte)0xD1, (byte)0x82, (byte)0x22, (byte)0x44, (byte)0x45, (byte)0x53, (byte)0x54, (byte)0x00, (byte)0x00});
    public static final GUID CAP_XTRAZ           = new GUID(new byte[]{(byte)0x1A, (byte)0x09, (byte)0x3C, (byte)0x6C, (byte)0xD7, (byte)0xFD, (byte)0x4E, (byte)0xC5, (byte)0x9D, (byte)0x51, (byte)0xA6, (byte)0x47, (byte)0x4E, (byte)0x34, (byte)0xF5, (byte)0xA0});
    
    public static GUID CAP_JIMM; // 		 = new GUID(new byte[]{'J', 'i', 'm', 'm', (byte)0x20, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00});

    public static final GUID CAP_QIP             = new GUID(new byte[]{(byte)0x56, (byte)0x3F, (byte)0xC8, (byte)0x09, (byte)0x0B, (byte)0x6F, (byte)0x41, 'Q', 'I', 'P', ' ', '2', '0', '0', '5', 'a'});
    public static final GUID CAP_QIPINFIUM	 = new GUID(new byte[]{(byte)0x7c, (byte)0x73, (byte)0x75, (byte)0x02, (byte)0xc3, (byte)0xbe, (byte)0x4f, (byte)0x3e, (byte)0xa6, (byte)0x9f, (byte)0x01, (byte)0x53, (byte)0x13, (byte)0x43, (byte)0x1e, (byte)0x1a});

    public static final GUID CAP_QIPSYMBIAN      = new GUID(Util.CAP_QIPSYMBIAN);
    public static final GUID CAP_QIPWM		 = new GUID(Util.CAP_QIPWM);
    public static final GUID CAP_VMICQ		 = new GUID(Util.CAP_VMICQ);

    public static final GUID CAP_MIRANDAIM       = new GUID(new byte[]{(byte)0x4D, (byte)0x69, (byte)0x72, (byte)0x61, (byte)0x6E, (byte)0x64, (byte)0x61, (byte)0x4D, (byte)0x00, (byte)0x06, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x03, (byte)0x08, (byte)0x07});
    public static final GUID CAP_ANDRQ           = new GUID(new byte[]{'&', 'R', 'Q', 'i', 'n', 's', 'i', 'd', 'e', (byte)8, (byte)8, (byte)9, (byte)0, (byte)0, (byte)0, (byte)0});
    public static final GUID CAP_KOPETE          = new GUID(new byte[]{'K', 'o', 'p', 'e', 't', 'e', ' ', 'I', 'C', 'Q', ' ', ' ', (byte) 0, (byte) 12, (byte) 0, (byte) 1});
    public static final GUID CAP_MACICQ          = new GUID(new byte[]{(byte)0xdd, (byte)0x16, (byte)0xf2, (byte)0x02, (byte)0x84, (byte)0xe6, (byte)0x11, (byte)0xd4, (byte)0x90, (byte)0xdb, (byte)0x00, (byte)0x10, (byte)0x4b, (byte)0x9b, (byte)0x4b, (byte)0x7d});
    public static final GUID CAP_MCHAT           = new GUID(new byte[]{'m','C','h','a','t',' ','i','c','q', (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0});

    public static void setUserInfo(GUID[] guids) throws JimmException 
    {
        // Send a CLI_SETUSERINFO packet
        // Set version information to this packet in our capability
        byte[] packet = new byte[guids.length * 16 + 4];
        packet[0] = 0x00;
        packet[1] = 0x05;
        packet[2] = (byte)((guids.length * 16) / 0x100);
        packet[3] = (byte)((guids.length * 16) % 0x100);
        for (int i = 0; i < guids.length; i++) 
        {
            System.arraycopy(guids[i].toByteArray(), 0, packet, i * 16 + 4, 16);
        }
        Icq.c.sendPacket(new SnacPacket(SnacPacket.CLI_SETUSERINFO_FAMILY, SnacPacket.CLI_SETUSERINFO_COMMAND, 0, new byte[0], packet));
    }

   /******************************************************************************************************** */     
    public static void setClientGUID(Vector guids) 
    {
        // init CAP_JIMM
        byte[] guid = new byte[]{'J', 'i', 'm', 'm', (byte)0x20, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00};

	byte[] ver = Util.stringToByteArray("###VERSION###"); //Options.getString(Options.OPTION_STRING_VERSION));
		
        System.arraycopy(ver, 0, guid, 5, ver.length <=11 ? ver.length : 11);
        CAP_JIMM = new GUID(guid);

        

        
        int client = Options.getInt(Options.OPTION_CLIENT_ID);
        switch (client) 
        {
            case  0: guids.addElement(CAP_JIMM); guids.addElement(new GUID(Util.CAP_JIMMDE));      break; // Jimm
            case  1: guids.addElement(CAP_MIRANDAIM);  break; // Miranda
			case 2: guids.addElement(CAP_QIP); guids.addElement(new GUID(Util.CAP_QIPPROTECTMSG)); guids.addElement(new GUID(Util.CAP_XTRAZ_SUPPORT)); break; // QIP 2005a
            case  3: guids.addElement(CAP_QIPINFIUM);  break; // QIP Infium
            case  4: guids.addElement(CAP_QIPSYMBIAN); break; // QIP Symb
            case  5: guids.addElement(CAP_QIPWM);      break; // QIP WinMobile
            case  6:                                   break; // stICQ
            case  7: guids.addElement(CAP_MCHAT);      break; // mChat
            case  8: guids.addElement(CAP_ANDRQ);      break; // &RQ
            case  9: guids.addElement(CAP_XTRAZ);      break; // XTraz for R&Q
            case 10: guids.addElement(CAP_KOPETE);     break; // Kopete
            case 11: guids.addElement(CAP_MACICQ);     break; // ICQ for MAC
            case 12: guids.addElement(CAP_VMICQ);     break; // VmICQ
            case 13:                                   break; // Unknown
        }

    }

    public static void setClientId() 
    {
        int client = Options.getInt(Options.OPTION_CLIENT_ID);
        long fp1 = 0xFFFFFFFE;
        long fp2 = 0x00010000;
        long fp3 = 0xFFFFFFFE;
        int prot = 9;
        
        switch (client) 
        {
            case  0: fp1 = 0xFFFFFFFE;  fp2 = 0x00010000; fp3 = 0xFFFFFFFE; prot =  9; break; // Jimm
            case  1: fp1 = 0xFFFFFFFF;  fp2 = 0x00030807; fp3 = 0xFFFFFFFF; prot =  8; break; // Miranda
            case  2: fp1 = 0x08000500;  fp2 = 0x0000000E; fp3 = 0x0000000F; prot = 11; break; // QIP 2005a
            case  3: fp1 = 0x0000232F;  fp2 = 0x00000000; fp3 = 0x00000000; prot = 11; break; // QIP Infium
            case  4: fp1 = 0x00000000;  fp2 = 0x00000000; fp3 = 0x00000000; prot = 11; break; // QIP SYMBIAN
            case  5: fp1 = 0x00000000;  fp2 = 0x00000000; fp3 = 0x00000000; prot = 11; break; // QIP Windows
            case  6: fp1 = 0x3BA8DBAF;  fp2 = 0x3BEB5373; fp3 = 0x3BEB5262; prot =  2; break; // stICQ
            case  7: fp1 = 0x00000000;  fp2 = 0x00000000; fp3 = 0x00000000; prot =  8; break; // mChat
            case  8: fp1 = 0xFFFFFF7F;  fp2 = 0x00090808; fp3 = 0x00000000; prot =  7; break; // &RQ
            case  9: fp1 = 0xFFFFF666;  fp2 = 0x0000042C; fp3 = 0x00000000; prot =  9; break; // R&Q
            case 10: fp1 = 0x00000000;  fp2 = 0x00000000; fp3 = 0x00000000; prot =  8; break; // Kopete
            case 11: fp1 = 0x00000000;  fp2 = 0x00000000; fp3 = 0x00000000; prot =  7; break; // Mac ICQ
            case 12: fp1 = 0x00000000;  fp2 = 0x00000000; fp3 = 0x00000000; prot =  8; break; // VmICQ
            case 13: fp1 = 0xFFFFF777;  fp2 = 0x0000142C; fp3 = 0x00000000; prot =  9; break; // Unknown
        }
        Util.putWord (CLI_SETSTATUS_DATA, 21, prot, true);
        Util.putDWord(CLI_SETSTATUS_DATA, 35,  fp1, true);
        Util.putDWord(CLI_SETSTATUS_DATA, 39,  fp2, true);
        Util.putDWord(CLI_SETSTATUS_DATA, 43,  fp3, true);
    }
   /******************************************************************************************************** */

    public static void setStandartUserInfo() throws JimmException 
    {
        GUID[] standart = new GUID[] {
                jimm.comm.OtherAction.CAP_AIM_ISICQ,
                jimm.comm.OtherAction.CAP_AIM_SERVERRELAY,
                jimm.comm.OtherAction.CAP_UNKNOWN,
                jimm.comm.OtherAction.CAP_UTF8};

        Vector guids = new Vector();
        setClientGUID(guids);

	int xstint = Options.getInt(Options.OPTION_XSTATUS);

        GUID xstatus = XStatus.getStatusGUID(xstint);

	Image xsi = XStatus.getStatusImage(xstint);

        if (xstatus != null && xsi != null) 
        {
            guids.addElement(xstatus);
        }

        if (Options.getBoolean(Options.OPTION_XTRAZ_ENABLE))
        guids.addElement(CAP_XTRAZ);

        //#sijapp cond.if modules_FILES="true" #
//        guids.addElement(CAP_FILE_TRANSFER);
        //#sijapp cond.end #
        //sijapp cond.if target isnot "DEFAULT"#
        if (Options.getInt(Options.OPTION_TYPING_MODE) != 0) 
        {
            guids.addElement(CAP_MTN);
        }
        //sijapp cond.end
        GUID[] result = new GUID[guids.size() + standart.length];
        guids.copyInto(result);
        System.arraycopy(standart, 0, result, guids.size(), standart.length);
        setUserInfo(result);
    }

    // CLI_SETSTATUS packet data
    public static byte[] CLI_SETSTATUS_DATA =
    	Util.explodeToBytes
    	(
    		"00,06,00,04,"+
    		"11,00,00,00,"+ // Online status
    		"00,0C,00,25,"+ // TLV(C)
    		"C0,A8,00,01,"+ // 192.168.0.1, cannot get own IP address
    		"00,00,AB,CD,"+ // Port 43981
    		"00,"+          // Firewall
    		"00,11,"+       // Support protocol version 11
    		"00,00,00,00,"+
    		"00,00,00,50,"+
    		"00,00,00,03,"+
    		"FF,FF,FF,FE,"+ // Timestamp 1
    		"00,01,00,00,"+ // Timestamp 2
    		"FF,FF,FF,FE,"+ // Timestamp 3
    		"00,00",
    		',', 16
    	);

    public static void setStatus(int status) throws JimmException 
    {
        // Send a CLI_SETSTATUS packet
        setClientId();
        Util.putDWord(CLI_SETSTATUS_DATA, 4, status);

	Util.putByte(CLI_SETSTATUS_DATA, 5, (Options.getBoolean(Options.OPTION_BIRTHDAY_BALLOON))? 0x09 : 0x01);

        Icq.c.sendPacket(new SnacPacket(SnacPacket.CLI_SETSTATUS_FAMILY, SnacPacket.CLI_SETSTATUS_COMMAND, 0, new byte[0], CLI_SETSTATUS_DATA));

    }

 }
